package org.middleware.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.middleware.common.AuthenticationToken.RequireLogin;
import org.middleware.common.BusinessException;
import org.middleware.controller.common.CommonHttpResponse;
import org.middleware.entity.system.SysEnterprises;
import org.middleware.service.system.SysEnterprisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequireLogin
@RestController
public class SysEnterprisesController {

    @Autowired
    private SysEnterprisesService enterprisesService;

    @GetMapping("/enterprises/{id}")
    public CommonHttpResponse getEnterpriseById(@PathVariable int id){
        try {
            SysEnterprises enterprise = enterprisesService.getEnterpriseByID(id);
            return CommonHttpResponse.success(enterprise);
        }catch (BusinessException e){
            log.error("企业查询失败：", e);
            return CommonHttpResponse.fail(400, e.getMessage());
        }
    }

    @GetMapping("/enterprises")
    public CommonHttpResponse getEnterprises() {
        try {
            List<SysEnterprises> result = enterprisesService.getEnterprises();
            return CommonHttpResponse.success(result);
        }catch (BusinessException e){
            log.error("企业查询失败：", e);
            return CommonHttpResponse.fail(400, e.getMessage());
        }
    }

    @PostMapping("/enterprises")
    public CommonHttpResponse createEnterprises(@RequestBody SysEnterprises enterprises) {
        try {
            enterprisesService.createEnterprise(enterprises);
            return CommonHttpResponse.success("企业新增成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("企业新增失败：", e);
            return CommonHttpResponse.fail(500, "企业新增失败，请联系管理员");
        }
    }

    @PutMapping("/enterprises")
    public CommonHttpResponse updateEnterprise(@RequestBody SysEnterprises enterprises) {
        log.info("请求参数为：", enterprises.toString());
        try{
            enterprisesService.updateEnterprise(enterprises);
            return CommonHttpResponse.success("修改成功");
        } catch (BusinessException e){
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("修改用户信息异常：", e);
            return CommonHttpResponse.fail(500, "修改信息异常，请联系管理员");
        }
    }

    @GetMapping("/enterprises/user/{id}/")
    public CommonHttpResponse getEnterprisesOfCurrentUser(@PathVariable int id, @RequestAttribute("isAdmin") int isAdmin) {
        try {
            List<SysEnterprises> result = enterprisesService.getEnterprisesOfCurrentUser(id, isAdmin);
            return CommonHttpResponse.success(result);
        } catch (Exception e) {
            log.error("搜索表单查询企业信息异常：", e);
            return CommonHttpResponse.fail(500, e.getMessage());
        }
    }
}
